package com.simpou.commons.utils.functional;

import lombok.RequiredArgsConstructor;

/**
 * Realiza uma ação de concatenar objetos de tipos quaisquer.
 *
 * @author Jonas Pereira
 * @since 2013-10-15
 */
@RequiredArgsConstructor
public class ConcatObjectAction<T> extends AbstractVoidActionIgnoreErrors<T> {

    private final String preffix;

    private final String suffix;
    /**
     * Usado em caso de iterações.
     */
    private final boolean endSuffixAllowed;

    private final Action<T, String> contentExtractor;

    private final StringBuilder result = new StringBuilder();

    public final String getResult() {
        final String returnAux;
        if (endSuffixAllowed) {
            returnAux = result.toString();
        } else {
            returnAux = result.length() > 0 ? result.substring(0, result.length() - suffix.length()) : result.toString();
        }
        return returnAux;
    }

    @Override
    protected void executeDelegate(T value) throws Exception {
        result.append(preffix).append(contentExtractor.execute(value)).append(suffix);
    }
    
    public void reset(){
        result.delete(0, result.length());
    }
}
